//
//  DHOrder_ReturnCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/18.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrder_ReturnCell.h"
#import "DHCompensateViewController.h"

@interface DHOrder_ReturnCell()

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *hintLabel;

@property (strong, nonatomic) NSDictionary *productDic;

@end

@implementation DHOrder_ReturnCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.returnbutton bk_addEventHandler:^(UIButton *sender) {
        if ([sender.titleLabel.text isEqualToString:@"归还"]) {
            sender.selected = !sender.selected;
            if (sender.selected) {
                sender.backgroundColor = [UIColor colorWithHexString:@"#1A82FF"];
                [self routerEventWithName:@"DHOrder_ReturnCell" userInfo:@{@"productDic":self.productDic,@"state":@"add"}];
            }else{
                sender.backgroundColor = [UIColor colorWithHexString:@"#ABB3BC"];
                [self routerEventWithName:@"DHOrder_ReturnCell" userInfo:@{@"productDic":self.productDic,@"state":@"delete"}];
            }
        }else if ([sender.titleLabel.text isEqualToString:@"赔偿"]){
            DHCompensateViewController *controller = [[DHCompensateViewController alloc] init];
            controller.dic = self.productDic;
            [self.viewController.navigationController pushViewController:controller animated:YES];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)dic{
    self.productDic = dic;
    NSDate *date = [NSDate dateWithServerISOString:[dic objectForKey:@"updatedAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    self.timeLabel.text = [NSDate stringFromDate:date withFormat:@"MM/dd/yyyy"];
    NSArray *snapshots = [dic objectForKey:@"snapshots"];
    if (snapshots.count > 0) {
        [self.imgView dh_setImageURLString:snapshots[0]];
    }else{
        [self.imgView dh_setImageURLString:@""];
    }
    self.nameLabel.text = [dic objectForKey:@"name"];
    NSArray *sizes = [dic objectForKey:@"sizes"];
    if (sizes.count > 0) {
        self.sizeLabel.text = [sizes[0] objectForKey:@"size"];
    }
    
    self.returnbutton.hidden = NO;
    if ([[dic objectForKey:@"return"] integerValue] == RetrunStateWaiting) {
        self.hintLabel.text = @"使用中";
        [self.returnbutton setBackgroundColor:[UIColor colorWithHexString:@"#ABB3BC"]];
        [self.returnbutton setTitle:@"归还" forState:UIControlStateNormal];
    }else if ([[dic objectForKey:@"return"] integerValue] == RetrunStateReturnning){
        self.hintLabel.text = @"归还中";
        [self.returnbutton setBackgroundColor:[UIColor colorWithHexString:@"#ABB3BC"]];
        [self.returnbutton setTitle:@"归还" forState:UIControlStateNormal];
    }else if ([[dic objectForKey:@"return"] integerValue] == RetrunStateCompleted){
        self.hintLabel.text = @"已归还";
        self.returnbutton.hidden = YES;
    }else if ([[dic objectForKey:@"return"] integerValue] == RetrunStateCompensation){
        self.hintLabel.text = @"损坏赔偿";
        [self.returnbutton setBackgroundColor:RGB(201, 0, 62)];
        [self.returnbutton setTitle:@"赔偿" forState:UIControlStateNormal];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
